const variables = this.getVariables();
const headers = { 'Content-Type': 'application/json' };
const proxy = variables.find(item => item.name === 'environmentType')?.value || '';

window.renderMonitorData = function() {
  const dataSource = [
    {
      "id": "0",
      "viewType": 0,
      "viewName": "公有视图",
      "monitorLevel": 'root',
      "children": []
    },
    {
      "id": "1",
      "viewType": 1,
      "viewName": "私有视图",
      "monitorLevel": 'root',
      "children": []
    }
  ];
  fetch(`/${proxy}SummaryAssessmentSV/videoView/list`, {
    method: 'POST',
    headers,
    body: JSON.stringify({ viewName: window._viewSearch || '' })
  }).then((res) => res.ok && res.json()).then(({ success, result = [] }) => {
    if (success) {
      dataSource.forEach(item => {
        item.children = result.filter(i => i.viewType == item.viewType);
      });
      const monitor = this.pageObj.getApiWidgetByAliasName('monitor').widgetObj;
      monitor._option.dataSource = dataSource;
      monitor.render();

      const monitorData = window._monitorSelect;
      if (monitorData?.id && monitorData?.operate !== 'delete') {
        const item = result.find(i => i.id === monitorData?.id);
        if (item) window._monitorSelect = item;
        const node = $(monitor.container).find(`#${window._monitorSelect.id}`);
        node.addClass('tree_node_active');
      } else {
        window._monitorSelect = undefined;
      }
    }
  });
};

window.getVideoData = function() {
  const widgets = this.getApiPage().getApiWidgetByAliasName('三维组件');
  const sullivan = widgets.widgetObj.sullivan;
  const players = sullivan.players;
  const $wrapper = sullivan.$content.find('.swiper-wrapper');
  const grid = $wrapper.attr('class').split('grid-')[1];
  let layout = grid.match(/\d+/g);
  const videoViewDetails = [];
  const $slide = $wrapper.find('.swiper-slide');
  $slide.each((index, dom) => {
    if (dom.classList.contains('has-player')) {
      const options = players.get(dom)?.__cache || {};
      videoViewDetails.push({
        location: index,
        equipmentId: options.equipmentCode,
        equipmentInfo: JSON.stringify(options),
      });
    }
  });
  return {
    videoViewDetails,
    layout: JSON.stringify(layout)
  }
}
